package com.pl.service.Impl;

import com.alibaba.excel.EasyExcel;
import com.pl.entity.SysUser;
import com.pl.entity.WmStorageLocation;
import com.pl.dao.WmStorageLocationDao;
import com.pl.lister.WmStorageLocationReadLocation;
import com.pl.lister.WmWarehouseReadLister;
import com.pl.service.WmStorageLocationService;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 库区表(WmStorageLocation)表服务实现类
 *
 * @author makejava
 * @since 2024-04-28 09:34:57
 */
@Service("wmStorageLocationService")
public class WmStorageLocationServiceImpl implements WmStorageLocationService {
    @Autowired
    private WmStorageLocationDao wmStorageLocationDao;

    /**
     * 通过ID查询单条数据
     *
     * @param locationId 主键
     * @return 实例对象
     */
    @Override
    public WmStorageLocation queryById(Long locationId) {
        return this.wmStorageLocationDao.queryById(locationId);
    }

    /**
     * 分页查询
     *
     * @return 查询结果
     */
    @Override
    public List<WmStorageLocation> queryByPage(Map<String, Object> map) {
        return this.wmStorageLocationDao.queryAllByLimit(map);
    }

    /**
     * 新增数据
     *
     * @param wmStorageLocation 实例对象
     * @return 实例对象
     */
    @Override
    public boolean insert(WmStorageLocation wmStorageLocation, HttpSession session) {
        SysUser user = (SysUser) session.getAttribute("user");
        wmStorageLocation.setCreateBy(user.getUserName());
        wmStorageLocation.setCreateTime(new Date());
        wmStorageLocation.setUpdateTime(new Date());
        int insert = this.wmStorageLocationDao.insert(wmStorageLocation);
        if (insert > 0) {
            return true;
        }
        return false;
    }

    /**
     * 修改数据
     *
     * @param wmStorageLocation 实例对象
     * @return 实例对象
     */
    @Override
    public boolean update(WmStorageLocation wmStorageLocation) {
        wmStorageLocation.setUpdateTime(new Date());
        int update = this.wmStorageLocationDao.update(wmStorageLocation);
        if (update > 0) {
            return true;
        }
        return false;
    }

    /**
     * 通过主键删除数据
     *
     * @param locationId 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(int locationId) {
        return this.wmStorageLocationDao.deleteById(locationId) > 0;
    }

    /**
     * 批量删除
     *
     * @param list
     * @return
     */
    @Override
    public boolean deleteBatch(List<Integer> list) {
        int num = wmStorageLocationDao.deleteBatch(list);
        if (num > 0) {
            return true;
        }
        return false;
    }

    /**
     * 批量添加
     * @param list
     * @return
     */
    @Override
    public boolean insertBatch(List<WmStorageLocation> list) {
        int i = wmStorageLocationDao.insertBatch(list);
        if (i>0)
        {
            return true;
        }        return false;
    }

    /**
     * 导入文件
     * @param file
     * @return
     */
    @Override
    public String importwmWarehouseLocationFile(MultipartFile file) {
        try {
            EasyExcel.read(file.getInputStream(),WmStorageLocation.class,new WmStorageLocationReadLocation(this)).sheet().doReadSync();
            return "import_success";
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "fail";
    }

    /**
     * 导出文件
     * @param response
     */
    @Override
    public void exportwmWarehouseLocationFile(HttpServletResponse response) {
        List<WmStorageLocation> wmStorageLocations=wmStorageLocationDao.finAll();
        try {
            EasyExcel.write(response.getOutputStream(),WmStorageLocation.class).sheet("库区信息").doWrite(wmStorageLocations);

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
